<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\StaffModel;

/**
 * 员工管理
 * Class Enter
 * @package Controller
 */
class Staff extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 添加员工
     * @param $_data
     * @return mixed
     */
    public function add($_data)
    {
        $_insert_data = array_merge(array(
            "uniacid" => $this->_uniacid,
            "uid" => $this->_uid,
            "time" => TIMESTAMP
        ),$_data);
        return StaffModel::create($_insert_data);
    }

    /**
     * 修改员工
     * @param $_data
     * @param $_staff_id
     * @return mixed
     */
    public function edit($_data,$_staff_id)
    {
        return StaffModel::where("id",$_staff_id)->update($_data);
    }

    /**
     * 通过上级boss用户id判断指定用户id是否为员工,并且返回数据
     * @param int $_boss_uid 上级用户id
     * @param int $_uid 用户id
     * @return StaffModel
     */
    public function testStaff($_boss_uid,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return StaffModel::where("uniacid",$this->_uniacid)
            ->where("boss",$_boss_uid)
            ->where("uid",$_uid)
            ->field(array("status","id"))
            ->find();
    }

    /**
     * 是否为员工
     * @param $_boss_uid
     * @param int $_uid
     * @return mixed
     */
    public function isStaff($_boss_uid,$_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return StaffModel::where("uniacid",$this->_uniacid)
            ->where("boss",$_boss_uid)
            ->where("uid",$_uid)
            ->where("status",1)
            ->value("id");
    }

    /**
     * 获取指定用户下的所有员工列表
     * @param int $_uid 用户uid
     * @return mixed
     */
    public function getUserStaffList($_uid = 0)
    {
        if(!$_uid) $_uid = $this->_uid;
        return StaffModel::where("boss",$_uid)
            ->where("status","<",9)
            ->field(array("id","status","uid"))
            ->order("status","desc")
            ->order("time","desc")
            ->select();
    }
}